package com.iris.egrant.dao.grantSetting;

import com.iris.egrant.exception.DaoException;
import com.iris.egrant.model.grantSetting.GrantSchedule;
import com.iris.egrant.model.grantSetting.GrantSetting;
import com.iris.egrant.orm.hibernate.HibernateDao;
import com.iris.egrant.utils.ConditionContainer;
import com.iris.egrant.grid.utils.PageContainer;
import com.iris.egrant.grid.utils.PageQueryHanlder;
import com.iris.egrant.utils.date.DateFormator;
import com.iris.egrant.utils.date.DateUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;


@Repository("grantSettingDao")
public class GrantSettingDao
        extends HibernateDao<GrantSetting, Long> {
    public List<GrantSetting> getGrantList() {
        /*  43 */
        String hql = " from GrantSetting where   enabled=1 and parentCode is null order by grantNo,seqNo";
        /*  44 */
        Query query = getSession().createQuery(hql);


        /*  47 */
        List<GrantSetting> list = query.list();
        /*  48 */
        return list;
    }


    public List<GrantSetting> getGrantSettingListByParentCodeNo(Long parentCode) {
        /*  58 */
        String hql = "from GrantSetting where   enabled=1 and parentCode=? order by grantNo,seqNo";
        /*  59 */
        List<GrantSetting> gsList = find(hql, new Object[]{parentCode});
        /*  60 */
        return gsList;
    }


    public int getGrantEnabled(List<Long> grantList) {
        /*  73 */
        String hql = " from GrantSetting where grantCode in (:grantCodeList) and enabled ='0'";
        /*  74 */
        Query query = getSession().createQuery(hql);
        /*  75 */
        query.setParameterList("grantCodeList", grantList);


        /*  78 */
        List<GrantSetting> list = query.list();
        /*  79 */
        return list.size();
    }


    public GrantSetting getGrantInfoByGrantNo(String grantNo) {
        /*  90 */
        return (GrantSetting) findUniqueBy("grantNo", grantNo);
    }


    public List<Long> getGrantCodes(Long grantCode) {
        /* 102 */
        GrantSetting gs = (GrantSetting) get(Long.valueOf(grantCode.longValue()));
        /* 103 */
        String hql = " select grantCode from GrantSetting where grantNo like :grantNo  and grantLevel<>3 and enabled=1 order by grantNo,seqNo";
        /* 104 */
        Query query = getSession().createQuery(hql);
        /* 105 */
        query.setParameter("grantNo", gs.getGrantNo() + "%");

        /* 107 */
        List<Long> list = query.list();
        /* 108 */
        return list;
    }


    public List<GrantSetting> getGrantListByParentCode(String grantCode) {
        /* 119 */
        if (!NumberUtils.isDigits(grantCode)) {
            /* 120 */
            return new ArrayList<GrantSetting>();
        }
        /* 122 */
        return findBy("parentCode", Long.valueOf(grantCode));
    }

    public long getCountForChild(Long grantCode) {
        /* 126 */
        String hql = " from GrantSetting where  enabled=1 and parentCode=?";
        /* 127 */
        return countHqlResult(hql, new Object[]{grantCode});
    }


    public PageContainer getTopGrantList(ConditionContainer conditionContainer) {
        /* 135 */
        Map<String, Object> conditionsMap = conditionContainer.getConditions();
        /* 136 */
        PageQueryHanlder pageQuery = new PageQueryHanlder();

        /* 138 */
        StringBuffer sb = new StringBuffer();
        /* 139 */
        sb.append("select  gs.grant_code as grant_code, gsc.gs_code as gs_code, gs.zh_cn_grant_name as grant_name, gs.grant_no as grant_no, to_char(gsc.end_date,'yyyy-mm-dd hh24:mi') as end_date, to_char(gsc.start_date,'yyyy-mm-dd hh24:mi') as start_date ");
        /* 140 */
        sb.append(" from grant_setting gs left outer join grant_schedule gsc on gs.grant_code=gsc.grant_code ");
        /* 141 */
        if (conditionsMap.containsKey("parentCode")) {
            /* 142 */
            String parentCode = (String) conditionsMap.get("parentCode");
            /* 143 */
            if ("null".equals(parentCode)) {
                /* 144 */
                sb.append("where gs.parent_code is null");
                /* 145 */
            } else if (NumberUtils.isDigits(parentCode)) {
                /* 146 */
                sb.append("where gs.parent_code = ?");
                /* 147 */
                pageQuery.addParam(Long.valueOf(parentCode));
            } else {
                /* 149 */
                sb.append("where gs.parent_code is not null");
            }

///* 152 */       if (!"null".equals(parentCode) && SecurityUtils.getCurrentUserRoleId() != 23) {
///*     */
///* 154 */         Long orgCode = SecurityUtils.getCurrentOrgCode();
///* 155 */         sb.append(" and gs.default_admin_org_code = ?");
///* 156 */         pageQuery.addParam(orgCode);
//      }
        }
        /* 159 */
        pageQuery.setQlMode("sql");
        /* 160 */
        pageQuery.addQlSegment(sb.toString());
        /* 161 */
        PageContainer p = pageQuery.loadOut(this, conditionContainer);
        /* 162 */
        return p;
    }


    public GrantSetting getGrantSettingByGrantCode(String grantCode) {
        /* 171 */
        if (grantCode == null || "".equals(grantCode.trim())) {
            /* 172 */
            return null;
        }
        /* 174 */
        String hql = "select p from GrantSetting p left join fetch p.grantSchedule pc where p.grantCode = ?";
        /* 175 */
        List<GrantSetting> grantSettings = getSession().createQuery(hql).setString(0, grantCode).list();
        /* 176 */
        return grantSettings.get(0);
    }


    public List<GrantSetting> getChildrenByGrantCode(String grantCode) {
        /* 185 */
        if (grantCode == null || "".equals(grantCode.trim())) {
            /* 186 */
            return null;
        }
        /* 188 */
        String hql = "select p from GrantSetting p left join fetch p.grantSchedule pc where p.parentCode = ?";
        /* 189 */
        List<GrantSetting> grantSettings = getSession().createQuery(hql).setString(0, grantCode).list();
        /* 190 */
        return grantSettings;
    }


    public void updateGrantSchedule(String grantCode, String startTime, String endTime) {
        /* 204 */
        if (grantCode != null && !"".equals(grantCode.trim())) {
            /* 205 */
            String hql = " from GrantSchedule where grantCode=?";
            /* 206 */
            List<GrantSchedule> list = getSession().createQuery(hql).setString(0, grantCode).list();
            /* 207 */
            if (!CollectionUtils.isEmpty(list)) {
                /* 208 */
                GrantSchedule gs = list.get(0);
                /* 209 */
                gs.setGrantCode(new Long(grantCode));
                /* 210 */
                gs.setStartDate(DateUtils.toDate(startTime, DateFormator.YEAR_MONTH_DAY));
                /* 211 */
                gs.setEndDate(DateUtils.toDate(endTime, DateFormator.YEAR_MONTH_DAY));
                /* 212 */
                gs.setUpdateDate(DateUtils.now());
///* 213 */         gs.setUpdatePsnCode(SecurityUtils.getCurrentUserId());
                /* 214 */
                getSession().update(gs);
            } else {
                /* 216 */
                GrantSchedule gs = new GrantSchedule();
                /* 217 */
                gs.setGrantCode(new Long(grantCode));
                /* 218 */
                gs.setStartDate(DateUtils.toDate(startTime, DateFormator.YEAR_MONTH_DAY));
                /* 219 */
                gs.setEndDate(DateUtils.toDate(endTime, DateFormator.YEAR_MONTH_DAY));
                /* 220 */
                gs.setUpdateDate(DateUtils.now());
///* 221 */         gs.setUpdatePsnCode(SecurityUtils.getCurrentUserId());
                /* 222 */
                getSession().save(gs);
            }
        }
    }


    public Long getChildrenGrantCount(String grantCode) {
        /* 234 */
        if (grantCode == null || "".equals(grantCode.trim())) {
            /* 235 */
            return null;
        }
        /* 237 */
        String hql = "select count(p) from GrantSetting p where p.parentCode = ?";
        /* 238 */
        Object grantCount = getSession().createQuery(hql).setLong(0, Long.valueOf(grantCode).longValue()).uniqueResult();
        /* 239 */
        return (Long) grantCount;
    }


    public List<GrantSetting> getAllGrantList() throws DaoException {
        /* 251 */
        StringBuilder sb = new StringBuilder();
        /* 252 */
        sb.append("select gs from  GrantSetting gs where gs.enabled=1 order by gs.grantNo");
        /* 253 */
        return getSession().createQuery(sb.toString()).list();
    }


    public PageContainer list4limitbyGrant(ConditionContainer conditionContainer) {
        /* 258 */
        Map<String, Object> conditionsMap = conditionContainer.getConditions();
        /* 259 */
        PageQueryHanlder pageQuery = new PageQueryHanlder();

        /* 261 */
        StringBuffer sb = new StringBuffer();
        /* 262 */
        sb.append("select gl.id,gs.grant_code,gs.zh_cn_grant_name grant_name, org.name org_name,gl.stat_year,gl.num from grant_limit gl,organization org,grant_setting gs ");
        /* 263 */
        sb.append(" where gl.org_code=org.org_code and gl.grant_code=gs.grant_code ");
        /* 264 */
        if (conditionsMap.containsKey("grantCode")) {
            /* 265 */
            String grantCode = (String) conditionsMap.get("grantCode");
            /* 266 */
            sb.append(" and gs.grant_code =? ");
            /* 267 */
            pageQuery.addParam(Long.valueOf(grantCode));
        }

        /* 270 */
        pageQuery.setQlMode("sql");
        /* 271 */
        pageQuery.addQlSegment(sb.toString());
        /* 272 */
        PageContainer p = pageQuery.loadOut(this, conditionContainer);
        /* 273 */
        return p;
    }


    public List<GrantSetting> getAllLeafGrantList() throws DaoException {
        /* 281 */
        StringBuilder sb = new StringBuilder();
        /* 282 */
        List<Object> params = new ArrayList();

        /* 284 */
        sb.append("select gs from  GrantSetting gs where gs.enabled=1 and gs.prpFormId<>0 and (gs.grantNo<>'00' or gs.grantNo is not null)");
///* 285 */     if (SecurityUtils.isAuthority("A_PRIVILEGE_XK")) {
///* 286 */       sb.append(" and gs.defaultAdminOrgCode = ?");
///* 287 */       params.add(SecurityUtils.getCurrentOrgCode());
//    }
        /* 289 */
        sb.append(" order by gs.zhCnGrantName");
        /* 290 */
        return createQuery(sb.toString(), params).list();
    }


    public Long getParentGrantCode(String parentName) {
        /* 295 */
        String sql = "select gs.grant_code from grant_setting gs where gs.zh_cn_grant_name=? and gs.grant_level=1 and rownum<=1";
        /* 296 */
        Object obj = getSession().createSQLQuery(sql).setString(0, parentName).uniqueResult();
        /* 297 */
        if (obj != null) {
            /* 298 */
            Long grantCode = Long.valueOf(obj.toString());
            /* 299 */
            return grantCode;
        }
        /* 301 */
        return null;
    }

    public Long getGrantCodes(String grantName) {
        /* 305 */
        String sql = "select gs.grant_code from grant_setting gs where gs.zh_cn_grant_name=? and gs.grant_level=2 and rownum<=1";
        /* 306 */
        Object obj = getSession().createSQLQuery(sql).setString(0, grantName).uniqueResult();
        /* 307 */
        if (obj != null) {
            /* 308 */
            Long grantCode = Long.valueOf(obj.toString());
            /* 309 */
            return grantCode;
        }
        /* 311 */
        return null;
    }

    public Long getGrantCodeByName(String grantName) {
        /* 315 */
        String sql = "select gs.grant_code from grant_setting gs where gs.zh_cn_grant_name=? and rownum<=1";
        /* 316 */
        Object obj = getSession().createSQLQuery(sql).setString(0, grantName).uniqueResult();
        /* 317 */
        if (obj != null) {
            /* 318 */
            Long grantCode = Long.valueOf(obj.toString());
            /* 319 */
            return grantCode;
        }
        /* 321 */
        return null;
    }

    public Long isExistGrantCodes(Long parentCode, Long grantCode) {
        /* 325 */
        String sql = "select gs.grant_code from grant_setting gs where gs.parent_code=? and gs.grant_code=?";
        /* 326 */
        Object obj = getSession().createSQLQuery(sql).setLong(0, parentCode.longValue()).setLong(1, grantCode.longValue()).uniqueResult();



        /* 330 */
        if (obj != null) {
            /* 331 */
            Long Code = Long.valueOf(obj.toString());
            /* 332 */
            return Code;
        }
        /* 334 */
        return null;
    }
}


/* Location:              D:\文件kjt\WEB-INF\lib\egrant-code-2.2.20240205.1659.jar!\com\iris\egrant\dao\grantSetting\GrantSettingDao.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       1.1.3
 */